package com.benyanyi.permissionlib.dialog;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.benyanyi.permissionlib.callback.PermissionCallBack;

import java.util.Locale;

/**
 * @author YanYi
 * @date 2022/8/31 2:05 下午
 * @email ben@yanyi.red
 * @overview
 */
public class PermissionDefaultDialogInfo {

    public static PermissionDialogInfo getInfo(final Activity activity) {
        //手机厂商
        final String brand = Build.BRAND;
        PermissionDialogInfo permissionDialogInfo = new PermissionDialogInfo();
        permissionDialogInfo.isShow = true;
        permissionDialogInfo.message = "权限不足，功能无法正常使用";
        permissionDialogInfo.title = "权限不足";
        permissionDialogInfo.negativeText = "取消";
        permissionDialogInfo.negativeClick = new PermissionDialogInfo.NegativeClick() {
            @Override
            public void onClick(PermissionCallBack callBack, String[] failurePermissions) {
                if (callBack != null) {
                    callBack.onFailure(failurePermissions);
                    callBack.onComplete();
                }
            }
        };
        permissionDialogInfo.positiveText = "跳转设置权限";
        permissionDialogInfo.positiveClick = new PermissionDialogInfo.PositiveClick() {
            @Override
            public void onClick(PermissionCallBack callBack, String[] failurePermissions) {
                if ("redmi".equalsIgnoreCase(brand) || "xiaomi".equalsIgnoreCase(brand.toLowerCase(Locale.ROOT))) {
                    gotoXMPermission(activity);
                } else if ("meizu".equalsIgnoreCase(brand)) {
                    gotoMZPermission(activity);
                } else if ("huawei".equalsIgnoreCase(brand) || "honor".equalsIgnoreCase(brand)) {
                    gotoHWPermission(activity);
                } else {
                    activity.startActivity(getAppDetailSettingIntent(activity));
                }
            }
        };
        return permissionDialogInfo;
    }

    /**
     * 跳转到miui的权限管理页面
     */
    private static void gotoXMPermission(Activity activity) {
        try {
            Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            intent.putExtra("extra_pkgname", activity.getApplicationContext().getPackageName());
            activity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                intent.putExtra("extra_pkgname", activity.getApplicationContext().getPackageName());
                activity.startActivity(intent);
            } catch (Exception e1) {
                e.printStackTrace();
                activity.startActivity(getAppDetailSettingIntent(activity));
            }
        }
    }

    /**
     * 跳转到魅族的权限管理系统
     */
    private static void gotoMZPermission(Activity activity) {
        try {
            Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra("packageName", activity.getApplicationContext().getPackageName());
            activity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            activity.startActivity(getAppDetailSettingIntent(activity));
        }
    }

    /**
     * 华为的权限管理页面
     */
    private static void gotoHWPermission(Activity activity) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //华为权限管理
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
            intent.setComponent(comp);
            activity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            activity.startActivity(getAppDetailSettingIntent(activity));
        }
    }

    /**
     * 获取应用详情页面intent（如果找不到要跳转的界面，也可以先把用户引导到系统设置页面）
     */
    private static Intent getAppDetailSettingIntent(Activity activity) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", activity.getApplicationContext().getPackageName(), null));
        } else {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", activity.getApplicationContext().getPackageName());
        }
        return localIntent;
    }

}
